package com.papper.common.service.base;

import com.papper.common.bean.base.BaseConstFile;
import com.papper.common.bean.base.BaseConstWorkdate;
import com.papper.common.cache.CacheConstants;
import com.papper.common.cache.redis.CacheClosureUnDefault;
import com.papper.common.mapper.base.BaseConstWorkdateMapper;
import com.papper.common.service.ServiceImpl;
import com.papper.common.utils.tool.DateTool;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

/**
 * @Author:LaJiao
 * @Date:2022-03-29 10:06:49
 * @Description:工作日
 */
@Service("baseConstWorkdateService")
public class BaseConstWorkdateService extends ServiceImpl<BaseConstWorkdateMapper, BaseConstWorkdate> {

    @Override
    protected void clearCaches(BaseConstWorkdate entity) {
        super.clearCaches(entity);
        String model = getModel();
        cache.delete(CacheConstants.Keys(model, entity.getDateDay()));
        String nowDay = DateTool.formatDate(new Date(), DateTool.TRADITION_PATTERN);
        cache.delete(CacheConstants.Keys(model, "T0", nowDay));
        cache.delete(CacheConstants.Keys(model, "T1", nowDay));
        cache.delete(CacheConstants.Keys(model, "MIN_T0", nowDay));
        cache.delete(CacheConstants.Keys(model, "MIN_T1", nowDay));
    }

    /**
     * function
     */
    public BaseConstWorkdate findWorkdate(final String workdate) {
        return cache.getAndSet(new CacheClosureUnDefault<BaseConstWorkdate>() {
            @Override
            public String getKey() {
                return CacheConstants.Keys(getModel(), workdate);
            }

            @Override
            public BaseConstWorkdate getValue() {
                Example          example  = new Example(BaseConstFile.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("dateDay", workdate);
                return findOne(example);
            }

            @Override
            public Class<BaseConstWorkdate> getTypeReference() {
                return BaseConstWorkdate.class;
            }
        });
    }

    public BaseConstWorkdate lastT0WordDate(final String maxDay) {
        return cache.getAndSet(new CacheClosureUnDefault<BaseConstWorkdate>() {
            @Override
            public String getKey() {
                return CacheConstants.Keys(getModel(), "TO", maxDay);
            }

            @Override
            public BaseConstWorkdate getValue() {
                Example          example  = new Example(BaseConstFile.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andLessThanOrEqualTo("dateDay", maxDay);
                criteria.andEqualTo("t0Status", 1);
                example.setOrderByClause("dateDay desc");
                return findOne(example);
            }

            @Override
            public Class<BaseConstWorkdate> getTypeReference() {
                return BaseConstWorkdate.class;
            }
        });
    }

    public BaseConstWorkdate lastT1WordDate(final String maxDay) {
        return cache.getAndSet(new CacheClosureUnDefault<BaseConstWorkdate>() {
            @Override
            public String getKey() {
                return CacheConstants.Keys(getModel(), "T1", maxDay);
            }

            @Override
            public BaseConstWorkdate getValue() {
                Example          example  = new Example(BaseConstFile.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andLessThanOrEqualTo("dateDay", maxDay);
                criteria.andEqualTo("t1Status", 1);
                example.setOrderByClause("dateDay desc");
                return findOne(example);
            }

            @Override
            public Class<BaseConstWorkdate> getTypeReference() {
                return BaseConstWorkdate.class;
            }
        });
    }

    public BaseConstWorkdate nextT0WordDate(final String minDay) {
        return cache.getAndSet(new CacheClosureUnDefault<BaseConstWorkdate>() {
            @Override
            public String getKey() {
                return CacheConstants.Keys(getModel(), "MIN_TO", minDay);
            }

            @Override
            public BaseConstWorkdate getValue() {
                Example          example  = new Example(BaseConstFile.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andGreaterThanOrEqualTo("dateDay", minDay);
                criteria.andEqualTo("t0Status", 1);
                example.setOrderByClause("dateDay asc");
                return findOne(example);
            }

            @Override
            public Class<BaseConstWorkdate> getTypeReference() {
                return BaseConstWorkdate.class;
            }
        });
    }

    public BaseConstWorkdate nextT1WordDate(final String minDay) {
        return cache.getAndSet(new CacheClosureUnDefault<BaseConstWorkdate>() {
            @Override
            public String getKey() {
                return CacheConstants.Keys(getModel(), "MIN_T1", minDay);
            }

            @Override
            public BaseConstWorkdate getValue() {
                Example          example  = new Example(BaseConstFile.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andGreaterThanOrEqualTo("dateDay", minDay);
                criteria.andEqualTo("t1Status", 1);
                example.setOrderByClause("dateDay asc");
                return findOne(example);
            }

            @Override
            public Class<BaseConstWorkdate> getTypeReference() {
                return BaseConstWorkdate.class;
            }
        });
    }

    public BaseConstWorkdate prevT1WordDate(final String minDay, final Integer preDay) {
        return cache.getAndSet(new CacheClosureUnDefault<BaseConstWorkdate>() {
            @Override
            public String getKey() {
                return CacheConstants.Keys(getModel(), "PREV_T1", minDay);
            }

            @Override
            public BaseConstWorkdate getValue() {
                Example          example  = new Example(BaseConstFile.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andLessThanOrEqualTo("dateDay", minDay);
                criteria.andEqualTo("t1Status", 1);
                example.setOrderByClause("dateDay desc");
                return findOne(example);
            }

            @Override
            public Class<BaseConstWorkdate> getTypeReference() {
                return BaseConstWorkdate.class;
            }
        });
    }
}
